﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace atmsystem_ss
{
    public partial class frmInformation_Account : Form
    {
        private SqlConnection con = new SqlConnection();
        private DataTable dt = new DataTable("tblAccount");
        private DataTable dtCustomer = new DataTable("tblCustomer");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;
        private void connect()
        {
            string cn = "Server=.\\SQLEXPRESS;Database=atmsystemsangson;User Id=sa;Password=123456";
            try
            {
                con = new SqlConnection(cn);
                con.Open();//mo ket noi

            }
            catch (Exception ex)
            {
                MessageBox.Show("Khong ket noi duoc co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void disconnect()
        {
            con.Close();//dong ket noi
            con.Dispose();//giai phong tai nguyen
            con = null;//huy doi tuong
           

        }
        private void getdata()
        {
            SqlCommand command = new SqlCommand();// khai bao 1 command
            command.Connection = con; //ket noi
            command.CommandType = CommandType.Text;//khai bao kieu command
            command.CommandText = @"Select  fldID_Account as N'AccountID',
                                            fldPIN as N'PIN',
                                            fldID_Customer as N'CustomerID',                                            
                                            fldCurrency as N'Currency',
                                            (Case fldStatus when 'True' then N'Work' else N'Key' end) as N'Status'
                                            From tblAccount";
            da.SelectCommand = command;//gan commad cho da
            da.Fill(dt);//nap du lieu cho table
            grvdata.DataSource = dt;//load du lieu len datagridview
            command.CommandText = "select * from tblCustomer";
            da.SelectCommand = command;
            da.Fill(dtCustomer);
            cboID_Customer.DataSource = dtCustomer;
            cboID_Customer.ValueMember = "fldID_Customer";
            cboID_Customer.SelectedValue = "fldID_Customer";

        }
        private void binding()
        {
            txtID_Account.DataBindings.Clear();
            txtID_Account.DataBindings.Add("Text", grvdata.DataSource, "AccountID");

            txtPIN.DataBindings.Clear();
            txtPIN.DataBindings.Add("Text", grvdata.DataSource, "PIN");

            cboID_Customer.DataBindings.Clear();
            cboID_Customer.DataBindings.Add("Text", grvdata.DataSource, "CustomerID");

            txtCurrency.DataBindings.Clear();
            txtCurrency.DataBindings.Add("Text", grvdata.DataSource, "Currency");

            cboStatus.DataBindings.Clear();
            cboStatus.DataBindings.Add("Text", grvdata.DataSource, "Status");
        }
        public frmInformation_Account()
        {
            InitializeComponent();
        }

        private void frmInformation_Account_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
            binding();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            disconnect();
            frmAdmin _frmAdmin = new frmAdmin();
            _frmAdmin.Show();

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            frmCreate_Account _frmCreate_Account = new frmCreate_Account();
            _frmCreate_Account.Show();
            Hide();
        }
        private bool kiemtra()
        {
            kt = true;
            if (txtID_Account.Text == "")
            {
                txtID_Account.Focus();
                MessageBox.Show("Ban chua nhap Account ID !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (txtPIN.Text == "")
            {
                txtPIN.Focus();
                MessageBox.Show("Ban chua nhap PIN !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
                
            else if (Convert.ToString(txtPIN.Text).Length.Equals(4)==false)
            {
                txtPIN.Focus();
                MessageBox.Show("Chu y PIN co 4 ky tu, moi nhap lai !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (cboID_Customer.Text == "")
            {
                cboID_Customer.Focus();
                MessageBox.Show("Ban chua chon Customer !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (txtCurrency.Text == "")
            {
                txtCurrency.Focus();
                MessageBox.Show("Ban chua nhap Currency !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (cboStatus.Text == "")
            {
                cboStatus.Focus();
                MessageBox.Show("Ban chua chon Status !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            return kt;
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            kiemtra();
            if (kt == true)
            {
                DataRow row = dt.Select("AccountID = '" + Convert.ToString(txtID_Account.Text) + "'")[0];
                row.BeginEdit();
                row["PIN"] = txtPIN.Text;
                row["CustomerID"] = cboID_Customer.SelectedValue;

                row["Currency"] = Convert.ToDecimal(txtCurrency.Text);
                if (cboStatus.Text == "Work")
                    row["Status"] = true;
                else if (cboStatus.Text == "Key")
                    row["Status"] = false;
                row.EndEdit();
                SqlCommand commandUpdate = new SqlCommand();
                commandUpdate.Connection = con;
                commandUpdate.CommandType = CommandType.Text;
                commandUpdate.CommandText = @"update tblAccount set 
                                                                fldPIN = @fldPIN,
                                                                fldID_Customer= @fldID_Customer,
                                                                fldCurrency = @fldCurrency,
                                                                fldStatus = @fldStatus
                                                                where fldID_Account= @fldID_Account";
                commandUpdate.Parameters.Add("@fldID_Account", SqlDbType.NVarChar, 50, "AccountID");

                commandUpdate.Parameters.Add("@fldPIN", SqlDbType.NVarChar, 50, "PIN");

                commandUpdate.Parameters.Add("@fldID_Customer", SqlDbType.NVarChar, 50, "CustomerID");

                commandUpdate.Parameters.Add("@fldCurrency", SqlDbType.Int, 50, "Currency");
                commandUpdate.Parameters.Add("@fldStatus", SqlDbType.Bit, 50, "Status");
                da.UpdateCommand = commandUpdate;
                da.Update(dt);
                MessageBox.Show("Ban da Update thanh cong", "Update successed", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
           
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("AccountID = '" + Convert.ToString(txtID_Account.Text)+"'")[0];
            row.BeginEdit();
            row.Delete();
            row.EndEdit();
            SqlCommand commandDelete = new SqlCommand();
            commandDelete.Connection = con;
            commandDelete.CommandType = CommandType.Text;
            commandDelete.CommandText = @"delete from tblAccount where fldID_Account = @fldID_Account";
            commandDelete.Parameters.Add("@fldID_Account", SqlDbType.NVarChar, 50, "AccountID");
            da.DeleteCommand = commandDelete;
            da.Update(dt);
            MessageBox.Show("Ban da Delete thanh cong", "Delete successed", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
